#__*__coding:utf-8__*__
#python3
import time
from tkinter import *

x=50
y=50

#（1）定义窗口
win=Tk()
win.title("移动小矩形块")
#（2）定义画布
canvas=Canvas(win,width=400,height=400) 
canvas.pack()#显示画布

#（3）定义矩形块
rect=canvas.create_rectangle(x, y, x+30, y+30,fill='red') 
print(rect)

#（4）定义移动小矩形的函数
def moveRect(event):#图片logo.gif的移动要绑定的函数
    if event.keysym=='Up':
        canvas.move(1,0,-3)#移动ID为1的事物，使得横坐标加0，纵坐标减3
    elif event.keysym=='Down':
        canvas.move(1,0,+3)
    elif event.keysym=='Left':
        canvas.move(1,-3,0)
    elif event.keysym=='Right':
        canvas.move(1,3,0)
    win.update()
    time.sleep(0.05)

#（5）绑定方向键
canvas.bind_all('<KeyPress-Up>',moveRect)
canvas.bind_all('<KeyPress-Down>',moveRect)
canvas.bind_all('<KeyPress-Left>',moveRect)
canvas.bind_all('<KeyPress-Right>',moveRect)

win.mainloop()
